// pages/cart/cart.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    paySpecs: false,

    specsNum: '',

    selectId: [],

    totalPrice: 0,

  },

  bindTips(e) {
    wx.showToast({
      title: '库存不足',
      icon: "loading",
      duration: 1200,
      mask: true,
    })
  },

  bindSpecs(e) {
    const that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var item = e.currentTarget.dataset.item;
    console.log(e)
    that.setData({
      specsNum: id,
      gs_id: id,
      specsInfo: item
    })
  },

  bindpaySpecs(e) {
    const that = this;
    var gid = e.currentTarget.dataset.gid;
    var indexNum = e.currentTarget.dataset.index;
    if (that.data.paySpecs == false) {
      wx.showLoading({
        title: '玩命加载中',
        mask: true,
      })
      wx.request({
        url: app.globalData.url + 'Goodsstyle',
        method: 'POST',
        data: {
          gid: gid,
        },
        success: function (res) {
          if (res.data.info.style.length != 0) {
            var specsInfo = res.data.info.style;
            var list = [];
            if (res.data.info.style.length != 0) {
              for (let k in res.data.info.style) {
                if (specsInfo[k].stock != 0) {
                  list.push(specsInfo[k]);
                  // console.log(list)
                }
              }
              that.setData({
                specsInfo: list[0],
                specsNum: list[0].id,
                indexNum
              })
            }
          }
          that.setData({
            specs: res.data.info.style,
            gid: gid,
            car_id: e.currentTarget.dataset.car_id,
            paySpecs: true
          })
          wx.hideLoading()
        },
      })
    } else {
      that.setData({
        paySpecs: false
      })
    }
  },

  bindCart(e) {
    const that = this;
    var selectId = that.data.selectId;
    var index = e.currentTarget.dataset.index;
    var stock = e.currentTarget.dataset.stock;
    var car_id = e.currentTarget.dataset.car_id;
    var totalPrice = that.data.totalPrice;
    var shopCar = that.data.shopCar;
    if (stock == 0) {
      wx.showToast({
        title: '库存不足',
        icon: "loading",
        duration: 1000,
        mask: true,
      })
      return
    } else if (shopCar[index].on == true) {
      shopCar[index].on = false
      selectId.splice(selectId.indexOf(shopCar[index].id), 1);
      that.getallprices()
    } else {
      shopCar[index].on = true
      selectId.push(shopCar[index].id);
      that.getallprices()
    }
    that.setData({
      shopCar,
      car_id: car_id,
      selectId: selectId,
    })
  },

  getallprices(e) {
    const that = this;
    var shopCar = that.data.shopCar;
    var totalPrice = 0;
    for (var a = 0; a < shopCar.length; a++) {
      if (shopCar[a].on == true) {
        let price = Number(shopCar[a].fee)
        totalPrice += parseFloat(price)
      }
    }
    that.setData({
      totalPrice: totalPrice
    })
  },

  bindDel(e) {
    const that = this;
    var index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '确定删除该宝贝?',
      content: '删除后无法恢复',
      showCancel: true,
      cancelText: '想一想',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '玩命加载中',
            mask: true,
          })
          wx.request({
            url: app.globalData.url + 'DelShopcar',
            data: {
              car_id: e.currentTarget.dataset.car_id,
            },
            method: 'POST',
            success: function (res) {
              if (res.data.info == true) {
                that.getShoopCar()
                wx.showToast({
                  title: '删除成功',
                  icon: "loading",
                  duration: 1000,
                  mask: true,
                })
              } else {
                wx.showToast({
                  title: '删除失败',
                  icon: "loading",
                  duration: 1000,
                  mask: true,
                })
              }
              wx.hideLoading()
            },
          })
        }
      },
    })
  },

  bindpurchase(e) {
    const that = this;
    var selectId = this.data.selectId;
    if (selectId.length == 0) {
      wx.showToast({
        title: '请选择至少一件商品',
        icon: 'none',
        duration: 1000,
        mask: true,
      })
    } else {
      wx.navigateTo({
        url: '../goods/goods-order?selectId=' + selectId + "&totalPrice=" + that.data.totalPrice,
      })
    }
  },

  navTo(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  getShoopCar(e) {
    const that = this;
    wx.showLoading({
      title: '玩命加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'Shopcar',
      data: {
        uid: app.globalData.uid,
      },
      method: 'POST',
      success: function (res) {
        if (res.data.info) {
          that.setData({
            shopCar: res.data.info.car,
            selectId: [],
          })
          that.getallprices()
        }
      },
    })
    wx.hideLoading()
  },

  bindRegister(e) {
    const that = this;
    var shopCar = that.data.shopCar;
    var indexNum = that.data.indexNum;
    var fee = e.currentTarget.dataset.fee;
    var style = e.currentTarget.dataset.style;
    wx.showLoading({
      title: '玩命加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'ChangeStyle',
      method: 'POST',
      data: {
        car_id: that.data.car_id,
        gs_id: that.data.gs_id,
      },
      success: function (res) {
        if (res.data.info) {
          // that.getShoopCar()
          that.setData({
            ['shopCar[' + indexNum + '].fee']: fee,
            ['shopCar[' + indexNum + '].style']: style,
            paySpecs: false,
          })
          that.getallprices()
          wx.showToast({
            title: '选择成功',
            icon: 'none',
            duration: 800,
            mask: true,
          })
        }
      },
    })
    wx.hideLoading()
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    wx.stopPullDownRefresh()
    if (app.globalData.uid) {
      needUid()
    } else {
      app.idCB = function (res) {
        needUid();
      }
    }

    function needUid() {
      wx.showLoading({
        title: '玩命加载中',
        mask: true,
      })
      wx.request({
        url: app.globalData.url + 'Shopcar',
        data: {
          uid: app.globalData.uid,
        },
        method: 'POST',
        success: function (res) {
          if (res.data.info) {
            that.setData({
              shopCar: res.data.info.car,
              selectId: [],
            })
            that.getallprices()
          }
        },
      })
      setTimeout(() => {
        wx.hideLoading()
      }, 500)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    if (app.globalData.buyNum == 1) {
      that.getShoopCar()
      that.setData({
        totalPrice: 0
      })
      app.globalData.buyNum = 0
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})